package cn.buk.qms.web;

import cn.buk.common.JsonResult;
import cn.buk.qms.service.*;
import cn.buk.tms.car.service.RentalCarService;
import cn.buk.tms.hotel.service.HotelOrderService;
import cn.buk.tms.train.service.TrainOrderService;
import cn.buk.tms.train.service.TrainRefundOrderService;
import cn.buk.tms.vas.service.VasOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import jakarta.servlet.http.HttpServletRequest;


/**
 *
 * @author yfdai
 */
@RestController
@RequestMapping("/admin")
public class AdminOrderController extends BaseController {

  @Autowired
  public AdminOrderController(FlightOrderService flightOrderService,
                              FlightRefundOrderService refundOrderService,
                              FlightChangeOrderService changeOrderService,

                              VasOrderService vasOrderService,
                              HotelOrderService hotelOrderService,
                              TrainOrderService trainOrderService,
                              TrainRefundOrderService trainRefundOrderService,
                              RentalCarService rentalCarService,
                              RollbackService rollbackService
                                    ) {
    this.flightOrderService = flightOrderService;
    this.refundOrderService = refundOrderService;
    this.changeOrderService = changeOrderService;

    this.vasOrderService = vasOrderService;
    this.hotelOrderService =hotelOrderService;
    this.trainOrderService = trainOrderService;
    this.trainRefundOrderService = trainRefundOrderService;
    this.rentalCarService = rentalCarService;

    this.rollbackService = rollbackService;
  }

  private final FlightOrderService flightOrderService;
  private final FlightRefundOrderService refundOrderService;
  private final FlightChangeOrderService changeOrderService;
  private final VasOrderService vasOrderService;

  private final HotelOrderService hotelOrderService;

  private final TrainOrderService trainOrderService;

  private final TrainRefundOrderService trainRefundOrderService;

  private final RentalCarService rentalCarService;

  private final RollbackService rollbackService;


  /**
   * 回滚机票订单的状态
   */
  @PostMapping("/flight/order/{id}/status/rollback")
  public JsonResult rollbackFlightOrderStatus(HttpServletRequest request,
      @PathVariable("id") int id,
      @RequestParam("remark") String remark) {
    final int enterpriseId = getEnterpriseId(request);
    final String username = getUsername(request);

    return rollbackService.rollbackFlightOrderStatus(enterpriseId, username, id, remark);
  }

  /**
   * 回滚退票单
   */
  @PostMapping("/refund/order/{id}/status/rollback")
  public JsonResult rollbackRefundOrderStatus(HttpServletRequest request,
                                              @PathVariable("id") int id,
                                              @RequestParam("remark") String remark) {
    final int enterpriseId = getEnterpriseId(request);
    final String username = getUsername(request);

    return refundOrderService.rollbackRefundOrderStatus(enterpriseId, username, id, remark);
  }



  /**
   * 回滚改签单
   */
  @PostMapping("/change/order/{id}/status/rollback")
  public JsonResult rollbackChangeOrderStatus(HttpServletRequest request,
                                              @PathVariable("id") int id,
                                              @RequestParam("remark") String remark) {
    final int enterpriseId = getEnterpriseId(request);
    final String username = getUsername(request);

    return changeOrderService.rollbackChangeOrderStatus(enterpriseId, username, id, remark);
  }


  /**
   * 回滚酒店订单的状态到上一个
   */
  @PostMapping("/hotel/order/{id}/status/rollback")
  public JsonResult rollbackHotelOrder(HttpServletRequest request,
                                       @PathVariable("id") int id,
                                       @RequestParam("remark") String remark) {
    final int enterpriseId = getEnterpriseId(request);
    final String username = getUsername(request);

    return hotelOrderService.rollbackHotelOrder(enterpriseId, username, id, remark);
  }

  /**
   * 回滚火车票订单的状态到上一个
   */
  @PostMapping("/train/order/{id}/status/rollback")
  public JsonResult rollbackTrainOrder(HttpServletRequest request,
                                       @PathVariable("id") int id,
                                       @RequestParam("remark") String remark) {
    final int enterpriseId = getEnterpriseId(request);
    final String username = getUsername(request);

    return trainOrderService.rollbackTrainOrder(enterpriseId, username, id, remark);
  }



  /**
   * 回滚火车票退票单的状态到上一个
   */
  @PostMapping("/train/refund/order/{id}/status/rollback")
  public JsonResult rollbackTrainRefundOrder(HttpServletRequest request,
                                             @PathVariable("id") int id,
                                             @RequestParam("remark") String remark) {
    final int enterpriseId = getEnterpriseId(request);
    final String username = getUsername(request);

    return trainRefundOrderService.rollbackOrder(enterpriseId, username, id, remark);
  }

  /**
   * 回滚租车订单状态到上一个
   */
  @PostMapping("/rental/car/order/{id}/status/rollback")
  public JsonResult rollbackRentalCarOrder(HttpServletRequest request,
                                           @PathVariable("id") int id,
                                           @RequestParam("remark") String remark) {
    final int enterpriseId = getEnterpriseId(request);
    final String username = getUsername(request);

    return rentalCarService.rollbackOrder(enterpriseId, username, id, remark);
  }


  /**
   * 回滚服务单的状态到上一个
   */
  @PostMapping("/vas/order/{id}/status/rollback")
  public JsonResult rollbackVasOrderStatus(HttpServletRequest request,
                                           @PathVariable("id") int id,
                                           @RequestParam("remark") String remark) {
    final int enterpriseId = getEnterpriseId(request);
    final String username = getUsername(request);

    return vasOrderService.rollbackVasOrderStatus(enterpriseId, username, id, remark);
  }
}
